package com.minyifang.wcspf.common.adapter;

import android.content.Context;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseMultiChooseAdapter extends RecyclerView.Adapter<BaseMultiChooseAdapter.Holder> {

    protected List<String> mStringList;
    private Context mContext;
    private List<Boolean> mBooleanList;

    public BaseMultiChooseAdapter(List<String> stringList, Context context) {
        mStringList = stringList;
        mContext = context;
        mBooleanList = new ArrayList<>();
        for (int i = 0; i < mStringList.size(); i++) {
            mBooleanList.add(false);
        }
    }

    public List<Integer> getChosePositionList() {
        List<Integer> positionList = new ArrayList<>();
        for (int i = 0; i < mBooleanList.size(); i++) {
            if (mBooleanList.get(i)) {
                positionList.add(i);
            }
        }
        return positionList;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(mContext).inflate(setLayoutResource(), parent, false);
        Holder holder = new Holder(layout);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mBooleanList.set(holder.getAdapterPosition(), !mBooleanList.get(holder.getAdapterPosition()));
                notifyDataSetChanged();
            }
        });
        return holder;
    }

    protected abstract int setLayoutResource();

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {
        boolean isSelected = mBooleanList.get(position);
        if (isSelected) {
            hasSelected(holder);
        } else {
            hasUnSelected(holder);
        }
        onBind(holder, position);
    }

    @Override
    public int getItemCount() {
        return mStringList.size();
    }

    protected abstract void onBind(Holder holder, int position);

    protected abstract void hasUnSelected(Holder holder);

    protected abstract void hasSelected(Holder holder);

    public class Holder extends RecyclerView.ViewHolder {

        Holder(View itemView) {
            super(itemView);
        }

        public void text(@IdRes int id, CharSequence text) {
            ((TextView) itemView.findViewById(id)).setText(text);
        }

        public void text(@IdRes int id, @StringRes int resid) {
            ((TextView) itemView.findViewById(id)).setText(resid);
        }

        public void image(@IdRes int id, String url) {
            Glide.with(mContext).load(url).into((ImageView) itemView.findViewById(id));
        }

        public void image(@IdRes int id, @DrawableRes int resid) {
            ((ImageView) itemView.findViewById(id)).setImageResource(resid);
        }

        @SuppressWarnings("unchecked")
        public <T extends View> T view(@IdRes int id) {
            return ((T) itemView.findViewById(id));
        }

//        public View view(@IdRes int id) {
//            return itemView.findViewById(id);
//        }

    }
}
